// cloudfunctions/getSellerGoods/index.js
const cloud = require('wx-server-sdk');
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
  })
const db = cloud.database();

exports.main = async (event, context) => {
  try {
    const { sellerId, status } = event;
    if (!sellerId) {
      return { success: false, message: '缺少卖家ID' };
    }
    
    // 构建查询条件
    const query = {
      sellerId: sellerId
    };
    if (status !== undefined) {
      query.status = status; // 是否在售
    }
    
    // 查询商品列表
    const goodsResult = await db.collection('blindbox_goods')
      .where(query)
      .orderBy('createTime', 'desc') // 按创建时间降序排列
      .get();
    
    return {
      success: true,
      data: goodsResult.data || []
    };
  } catch (error) {
    console.error('获取卖家商品失败', error);
    return { success: false, message: '服务器错误', error: error.message };
  }
};